package com.collectplus.express.passport;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.media.Image;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.collectplus.express.R;
import com.collectplus.express.evaluate.EvaluateActivity;
import com.collectplus.express.model.PassportDetailBean;
import com.collectplus.express.parcel.ParcelDetailActivity;

import droid.frame.utils.http.BitmapUtils;


/**
 * passport 上门路由的查看物流，二维码放在这里面操作
 * Created by lidandan on 2017/12/16.
 */

public class PassportDoorRouteOperate implements View.OnClickListener {
    private PassportDetailBean item;
    // 操作按钮 ：去评价、查看物流
    private TextView mperateView;
    private View mLayout = null;
    private Activity mContext;

    public PassportDoorRouteOperate(Activity context, View view) {
        this.mContext = context;
        this.mLayout = view;
    }

    public void loadLayout(PassportDetailBean item) {
        mLayout.setVisibility(View.VISIBLE);
        this.item = item;
        //新需求修改，寄件码在文案中一起返回
        //        TextView codeView = (TextView) findViewById(R.id.parcel_code);
        //状态描述
        TextView parcelRoute = (TextView) mLayout.findViewById(R.id.passport_door_route);
        TextView sendCode = (TextView) mLayout.findViewById(R.id.passport_door_route_outtime_message);
        ImageView qrcodeIv = (ImageView) mLayout.findViewById(R.id.passport_door_qrcode_iv);
        mperateView = (TextView) mLayout.findViewById(R.id.parcel_door_route_operation);
        mperateView.setOnClickListener(this);
        parcelRoute.setText(item.getStatusDesc());
        if (item.getPassportStatus() == 1) {
            //            codeView.setText(item.getCheckCode());
            sendCode.setText(item.getCheckCode());
            sendCode.setVisibility(View.VISIBLE);
            qrcodeIv.setImageBitmap(BitmapUtils.base64ToBitmap(item.getQrcode()));
            mperateView.setVisibility(View.GONE);
        } else if (item.getPassportStatus() == 3) {
            mperateView.setVisibility(View.GONE);
            sendCode.setVisibility(View.GONE);
        } else if (item.getPassportStatus() == 4) {
            mperateView.setVisibility(View.VISIBLE);
            sendCode.setVisibility(View.GONE);
        } else if (item.getPassportStatus() == 5) {
            mperateView.setVisibility(View.VISIBLE);
            sendCode.setVisibility(View.GONE);
        }
        handleRouteState();
    }

    /**
     * 处理路由
     */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void handleRouteState() {
        // 状态文本
        TextView statusText = (TextView) mLayout.findViewById(R.id.passport_door_state);
        //驿站投递包裹指南，只在待投递显示
        if (item.getIsVisit()) {
            if (item.getPassportStatus() == 1) {
                setParcelProgressBar(2, 0, 0, 0);
                statusText.setText("待投递");
            } else if (item.getPassportStatus() == 3) {
                setParcelProgressBar(2, 2, 0, 0);
                statusText.setText("待交接");
            } else if (item.getPassportStatus() == 4) {
                setParcelProgressBar(2, 2, 2, 0);
                statusText.setText("待送达");
                mperateView.setText("查看物流");
            } else if (item.getPassportStatus() == 5) {
                setParcelProgressBar(2, 2, 2, 2);
                statusText.setText("待评价");
                mperateView.setText("去评价");
            }
        }
    }

    /**
     * 物流状态条颜色
     */
    @SuppressWarnings("deprecation")
    private void setParcelProgressBar(int... colors) {
        ImageView item1 = (ImageView) mLayout.findViewById(R.id.passport_door_progress_1);
        ImageView item2 = (ImageView) mLayout.findViewById(R.id.passport_door_progress_2);
        ImageView item3 = (ImageView) mLayout.findViewById(R.id.passport_door_progress_3);
        ImageView item4 = (ImageView) mLayout.findViewById(R.id.passport_door_progress_4);

        GradientShape shape1 = new GradientShape(mContext);
        GradientShape shape2 = new GradientShape(mContext);
        GradientShape shape3 = new GradientShape(mContext);
        GradientShape shape4 = new GradientShape(mContext);

        shape1.setStyle(0, colors[0]);
        shape2.setStyle(1, colors[1]);
        shape3.setStyle(1, colors[2]);
        shape4.setStyle(2, colors[3]);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            item1.setBackground(shape1);
            item2.setBackground(shape2);
            item3.setBackground(shape3);
            item4.setBackground(shape4);
        } else {
            item1.setBackgroundDrawable(shape1);
            item2.setBackgroundDrawable(shape2);
            item3.setBackgroundDrawable(shape3);
            item4.setBackgroundDrawable(shape4);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.parcel_door_route_operation:
                // 查看物流
                if (item.getPassportStatus() == 4) {
                    Intent intent = new Intent(mContext, ParcelDetailActivity.class);
                    intent.putExtra("parcelStatus", item.getParcelStatus());
                    intent.putExtra("parcelId", item.getParcelId());
                    intent.putExtra("from", 1);
                    mContext.startActivity(intent);
                    // 服务评论
                } else if (item.getPassportStatus() == 5) {
                    Intent intent = new Intent(mContext, EvaluateActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("orderId", item.getOrderId());
                    intent.putExtras(bundle);
                    mContext.startActivityForResult(intent, 1001);
                }
                break;
            default:
                break;
        }
    }
}
